package com.cap.dreamcircle.App;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Environment;

import com.cap.dreamcircle.Model.Action.KeyCounterAction;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.IUserModel;
import com.cap.dreamcircle.Model.Impl.UserModelImpl;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.CrashHandler;
import com.cap.dreamcircle.Utils.PreferenceUtils;
import com.cap.dreamcircle.Utils.TransformUtils;
import com.github.sahasbhop.apngview.ApngImageLoader;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;
import de.greenrobot.event.EventBus;

/**
 * Application类
 * Created by liukanshan on 2016/12/10.
 */

public class App extends Application {

    public static App instance;
    private IUserModel userModel;
    /**
     * 打开的activity
     **/
    private List<Activity> activities = new ArrayList<Activity>();

    public static App GetInstance() {
        synchronized (App.class) {
            if (instance == null) {
                instance = new App();
            }
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        PreferenceUtils.SetStartCount(PreferenceUtils.GetStartCount() + 1);
        initSmartRefreshLayoutHeaderFooter();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
        Thread.setDefaultUncaughtExceptionHandler(CrashHandler.getInstance());
        //bugly初始化
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(instance); //App的策略Bean
        strategy.setAppChannel(getPackageName());     //设置渠道
        //strategy.setAppVersion(getVersion());      //App的版本
        strategy.setAppReportDelay(1000);  //设置SDK处理延时，毫秒
        strategy.setDeviceID(android.os.Build.MODEL + ";" + android.os.Build.VERSION.SDK_INT + ";" + android.os.Build.VERSION.RELEASE);
        CrashReport.initCrashReport(instance, "03228cf269", true, strategy);
        EventBus.getDefault().register(this);
        ApngImageLoader.getInstance().init(getApplicationContext());
        JPushInterface.setDebugMode(false);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush
    }

    private void initSmartRefreshLayoutHeaderFooter() {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(android.R.color.transparent, android.R.color.transparent);//全局设置主题颜色
                //return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header，默认是 贝塞尔雷达Header
                return new MaterialHeader(context);
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
                //return new footer
            }
        });
    }

    /**
     * 新建了一个activity
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    /**
     * 结束指定的Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            this.activities.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 应用退出，结束所有的activity
     */
    public void exit() {
        for (Activity activity : activities) {
            if (activity != null) {
                activity.finish();
            }
        }
        System.exit(0);
    }

    /**
     * 关闭Activity列表中的所有Activity
     */
    public void finishActivity() {
        for (Activity activity : activities) {
            if (null != activity) {
                activity.finish();
            }
        }
        //杀死该应用进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishClassActivity(Class<?> cls) {
        for (Activity activity : activities) {
            if (activity.getClass().getName().equals(cls.getName())) {
                finishActivity(activity);
                return;
            }
        }
    }

    public void finishOtherActivity(Class<?> cls) {
        for (Activity activity : activities) {
            if (!activity.getClass().getName().equals(cls.getName())) {
                finishActivity(activity);
                return;
            }
        }
    }

    public int getActivityCount() {
        if (activities == null) {
            return 0;
        } else {
            return activities.size();
        }
    }

    public String getVoiceDir() {
        return Environment.getExternalStorageDirectory().getPath() + "/dreamCircle/voice";
    }

    public List<Activity> getActivities() {
        return activities;
    }

    public void onEvent(KeyCounterAction action) {
        if (userModel == null) {
            userModel = new UserModelImpl();
        }
        userModel.CountKey(action.getKey()).compose(TransformUtils.<BaseBean>defaultSchedulers()).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {

            }

            @Override
            public void onFailure(BaseBean response) {

            }
        });
    }
}
